package com.k2data.k2app.service;


import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.k2data.k2app.common.MyConstant;
import com.k2data.k2app.domain.deviceRest.Event;
import com.k2data.k2app.domain.deviceRest.MonitorLimitValue;
import com.k2data.k2app.exception.K2ResponseException;
import com.k2data.k2app.rest.DeviceClient;
import com.k2data.k2app.rest.DeviceClientV2;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 从device模块取回来的数据
 * Created by clb on 17-3-30.
 */
@Log4j2
@Service
public class DeviceService {

    @Autowired
    DeviceClient deviceClient;
    @Autowired
    DeviceClientV2 deviceClientV2;
    @Autowired
    MyConstant myConstant;
    public DeviceService() {
    }

    public List getCustomers() throws Exception{
        if(myConstant.getGroupIdRule() == null || "2".equals(myConstant.getGroupIdRule())){
            return Lists.newArrayList();
        }else {
            return deviceClientV2.getCustomers(null,null,null,null,null,null,null,null,null,1,10000,null).getResult();
        }
    }


    public Map getCustomerById(Long customerId) throws Exception{
        if(myConstant.getGroupIdRule() == null || "2".equals(myConstant.getGroupIdRule())){
            return deviceClient.getCustomerById(customerId).getResult();
        }else {
            return deviceClientV2.getCustomerById(customerId).getResult();
        }
    }

    public Map addEvent(Event event){
        if(myConstant.getGroupIdRule() == null || "2".equals(myConstant.getGroupIdRule())){
            return deviceClient.addEvent(event).getResult();
        }else {
            return deviceClientV2.addEvent(event).getResult();
        }
    }

    public List  getDevices() throws Exception{
        if(myConstant.getGroupIdRule() == null || "2".equals(myConstant.getGroupIdRule())){
            return Lists.newArrayList();
        }else {
            return deviceClientV2.getAllDevices(1,10000,null).getResult();
        }
    }

    public List  getDevicesByCustomer(Long customerId) throws Exception{
        if(myConstant.getGroupIdRule() == null || "2".equals(myConstant.getGroupIdRule())){
            return Lists.newArrayList();
        }else {
            return deviceClientV2.getDeviceByCustomer(customerId).getResult();
        }
    }


    public Map findDeviceByCode(String code) throws Exception{
        List list= Lists.newArrayList();
        if(myConstant.getGroupIdRule() == null || "2".equals(myConstant.getGroupIdRule())){
            list =  deviceClient.getAllDevices(1,4000,code).getResult();
        }else {
            list =  deviceClientV2.getAllDevices(1,4000,code).getResult();
        }
        if(list.size()>0){
            return (Map)list.get(0);
        }
        return null;
    }

    public Map getDeviceById(Long id) throws Exception{
        if(myConstant.getGroupIdRule() == null || "2".equals(myConstant.getGroupIdRule())){
            return deviceClient.getDeviceById(id).getResult();
        }else {
            return deviceClientV2.getDeviceById(id).getResult();
        }
    }

    public Map updateDevice(Long id,Object device) throws Exception{
        if(myConstant.getGroupIdRule() == null || "2".equals(myConstant.getGroupIdRule())){
            return deviceClient.updateDevice(id,device).getResult();
        }else {
            return deviceClientV2.updateDevice(id,device).getResult();
        }
    }

    public void changeStatus(String deviceNo, String status) throws Exception{
        if(myConstant.getGroupIdRule() == null || "2".equals(myConstant.getGroupIdRule())){
              deviceClient.changeStatus(deviceNo,status);
        }else {
              deviceClientV2.changeStatus(deviceNo,status);
        }
    }

    public Map getTemplateDeviceById(Long id) throws K2ResponseException{
        List list= Lists.newArrayList();
        if(myConstant.getGroupIdRule() == null || "2".equals(myConstant.getGroupIdRule())){
            list =  deviceClient.getTemplateDeviceById(id).getResult();
        }else {
            list =  deviceClientV2.getTemplateDeviceById(id).getResult();
        }
        if(list.size()>0) {
            return (Map) list.get(0);
        }
        return new HashMap(16);
    }

    public Map getTempplatePoint(Long id) throws K2ResponseException {
        List list= Lists.newArrayList();
        if(myConstant.getGroupIdRule() == null || myConstant.getGroupIdRule().equals("2")){
            list =  deviceClient.getTemplatePointById(id).getResult();
        }else {
            list =  deviceClientV2.getTemplatePointById(id).getResult();
        }
        if(list.size()>0){
            return (Map) list.get(0);

        }
        return new HashMap(16);
    }

    public Map getDevicePointById(Long id) throws K2ResponseException {
        if(myConstant.getGroupIdRule() == null || myConstant.getGroupIdRule().equals("2")){
            return deviceClient.getDevicePointById(id).getResult();
        }else {
            return deviceClientV2.getDevicePointById(id).getResult();
        }
    }

    public Map getDevicePoint(Long deviceId,Long monitorPointId,Integer channelId) throws K2ResponseException {
        List list= Lists.newArrayList();
        if(myConstant.getGroupIdRule() == null || myConstant.getGroupIdRule().equals("2")){
            list =  deviceClient.getDevicePoint(deviceId,monitorPointId,channelId,false).getResult();
        }else {
            list =  deviceClientV2.getDevicePointGroup(deviceId,monitorPointId,channelId,false,1).getResult();
        }
        if(list.size()>0){
            return (Map) list.get(0);

        }
        return new HashMap(16);
    }

    public Map getDevicePointGroup(Long deviceId,Long monitorPointId,Integer channelId,boolean isGroup,Integer groupType) throws K2ResponseException {
        List list= Lists.newArrayList();
        if(myConstant.getGroupIdRule() == null || myConstant.getGroupIdRule().equals("2")){
            list =  deviceClient.getDevicePoint(deviceId,monitorPointId,channelId,isGroup).getResult();
        }else {
            list = deviceClientV2.getDevicePointGroup(deviceId,null,channelId,isGroup,groupType).getResult();
        }
        if(list.size()>0){
            if(isGroup && groupType == 2 && monitorPointId != null){
                Map map2 = getDevicePoint(deviceId,monitorPointId,null);
                if(map2.size()>0){
                    for(int i =0 ;i<list.size() ;i++){
                        Map map3 = (Map)list.get(i);
                        if(map3.get("nameEn").toString().equals(map2.get("nameEn").toString())){
                            return map3;
                        }
                    }
                }
            }
            return (Map) list.get(0);
        }
        return new HashMap(16);
    }

    public Map getTemplateVariablesById(Long id) throws K2ResponseException {
        List list= Lists.newArrayList();
        if(myConstant.getGroupIdRule() == null || myConstant.getGroupIdRule().equals("2")){
            list =  deviceClient.getTemplateVariablesById(id).getResult();
        }else {
            list =  deviceClientV2.getTemplateVariablesById(id).getResult();
        }
        if(list.size()>0){
            return (Map) list.get(0);

        }
        return new HashMap(16);
    }

    public Map getDeviceVariablesById(Long id) throws K2ResponseException {
        List list= Lists.newArrayList();
        if(myConstant.getGroupIdRule() == null || myConstant.getGroupIdRule().equals("2")){
            list =  deviceClient.getDeviceVariablesById(id).getResult();
        }else {
            list =  deviceClientV2.getDeviceVariablesById(id).getResult();
        }
        if(list.size()>0){
            return (Map) list.get(0);

        }
        return new HashMap(16);
    }

    public List getDeviceLimitValues(Long variableId,String deviceNo,Integer channelId,boolean isTemplateVariable) throws K2ResponseException {
        List list= Lists.newArrayList();
        if(myConstant.getGroupIdRule() == null || myConstant.getGroupIdRule().equals("2")){
        }else {
            list =  deviceClientV2.getDeviceLimitValues(null,null,variableId,deviceNo,channelId,null,isTemplateVariable).getResult();
        }
        return list;
    }

    public void updateLimitValue(Long limitId, MonitorLimitValue monitorLimitValue,Boolean isLimt2Kmx) throws K2ResponseException {
        if(myConstant.getGroupIdRule() == null || myConstant.getGroupIdRule().equals("2")){
        }else {
            deviceClientV2.updateMonitorLimit(limitId,monitorLimitValue,isLimt2Kmx).getResult();
        }
    }
    public Map getContactById(Long id) throws Exception{
        if(myConstant.getGroupIdRule() == null || "2".equals(myConstant.getGroupIdRule())){
            return Maps.newHashMap();
        }else {
            return deviceClientV2.getContactById(id).getResult();
        }
    }

}
